package com.lx.sum;

import com.lx.entitys.CovidSumEntity;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: chenjiang
 * @Date: 2021/11/18/16:34
 * @Description:
 */
public class CovidSumMapper extends Mapper<LongWritable, Text, Text, CovidSumEntity> {
    private Text outKey = new Text();
    private CovidSumEntity outValue = new CovidSumEntity();

    /***
     * @param key 文本数据偏移量
     * @param value  文本数据
     * @param context 上下文
     * @throws IOException
     * @throws InterruptedException
     * @desc: 数据格式如下
     * 时间 县 州 编码 确诊数 死亡数
     * <br> 2021-01-28,Houston,Alabama,01069,9301,190</br>
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String values = new String(value.getBytes(),0,value.getLength(),"UTF-8");
//        String values = value.toString();

        String[] split = values.split(",");
        outKey.set(split[2]);
        outValue.setCases(Long.valueOf(split[split.length - 2]));
        outValue.setDeaths(Long.valueOf(split[split.length - 1]));
        context.write(outKey, outValue);
    }
}
